package com.example.anliku.service.serviceImpl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.anliku.entity.Clazz;
import com.example.anliku.entity.ExampleKnowledge;
import com.example.anliku.entity.Message;
import com.example.anliku.mapper.ExampleKnowledgeMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.anliku.entity.Knowledge;
import com.example.anliku.mapper.KnowledgeMapper;
import com.example.anliku.service.KnowledgeService;

import java.util.List;

@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Resource
    private KnowledgeMapper knowledgeMapper;
    @Resource
    private ExampleKnowledgeMapper exampleKnowledgeMapper;

    @Override
    public Message deleteByPrimaryKey(Integer knowledgeId) {
        List<String> knowName=knowledgeMapper.selectKnowledgeNameByKnowledgeId(knowledgeId);
        int a=exampleKnowledgeMapper.deleteByExampleKnowledgeName(knowName.get(0));
        int b=knowledgeMapper.deleteByPrimaryKey(knowledgeId);
        if(a>0||b>0) {
            return Message.success(null);
        }
        else{
            return Message.fail(null);
        }
    }

    @Override
    public Message insert(Knowledge record) {
        return knowledgeMapper.insert(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message insertSelective(Knowledge record) {
        List <Knowledge> knowledgeList=knowledgeMapper.selectAllByKnowledgeName(record.getKnowledgeName());
        if(CollectionUtil.isNotEmpty(knowledgeList)){
            return Message.fail("知识点标签重复");
        }
        return knowledgeMapper.insertSelective(record)>0?Message.success(null).add(record.getKnowledgeId()):Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer knowledgeId) {
        return Message.success(null).add(knowledgeMapper.selectByPrimaryKey(knowledgeId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Knowledge record) {
        List<ExampleKnowledge> exampleKnowledges=exampleKnowledgeMapper.selectAllByExampleKnowledgeName(knowledgeMapper.selectByPrimaryKey(record.getKnowledgeId()).getKnowledgeName());
        System.out.println(exampleKnowledges);
        for(ExampleKnowledge exampleKnowledge:exampleKnowledges) {
            exampleKnowledge.setExampleKnowledgeName(record.getKnowledgeName());
            exampleKnowledgeMapper.updateByPrimaryKeySelective(exampleKnowledge);
        }
        knowledgeMapper.updateByPrimaryKeySelective(record);
        return Message.success(null);
    }

    @Override
    public Message updateByPrimaryKey(Knowledge record) {
        return knowledgeMapper.updateByPrimaryKey(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(knowledgeMapper.selectAll());
    }

}


